<?php
if (!defined('LEGAL_INNER_USE')){die('ILLEGAL USING!');}


function makeUrl($main_page = 'index', $para = ''){
	if (defined('ENABLE_STATIC_URL') && ENABLE_STATIC_URL){
		if ($para != ''){
			$para = explode('&', $para);
			#dump($para);exit;
			if (!empty($para)){
				foreach($para as $v){
					$v = explode('=', $v);
					if (!empty($v) && sizeof($v)==2){
						try {
							eval('$'.$v[0].'="'.$v[1].'";');
						} catch (Exception $e) {
							dump($e);
						}
					}
				}
			}
		}
		switch($main_page){
			case 'products':
				$cateId = $cateId*1;
				$page = $page*1;
				$page = $page<1?1:$page;
				
				$r = HTTP_ROOT."category-$cateId-$page.html";
				break;
			case 'pInfo':
				$pID = $pID*1;
				
				$r = HTTP_ROOT."product-$pID.html";
				break;
			case 'goodsToKnow':
				if (isset($no) && !empty($no)){
					$r = HTTP_ROOT.$main_page.'-'.$no.'.html';
				} else {
					$r = HTTP_ROOT.$main_page.'.html';
				}
				break;
			case 'searchResult':
				$k = urlencode(str_replace('-', '', $k));
				$page = $page*1;
				$page = $page<1?1:$page;

				$r = HTTP_ROOT."search-$k-$page.html";
				break;
			case 'officialShopProductInfo':
				$r = HTTP_ROOT.OFFICIAL_SHOP_DIR."goods-$id-$title.html";
				break;
			default:
				$r = HTTP_ROOT.$main_page.'.html';
		}
		return $r;
	} else {
		switch($main_page){
			case 'index':
				$r = HTTP_ROOT;
				break;
			case 'officialShopProductInfo':
				$r = HTTP_ROOT.OFFICIAL_SHOP_DIR.'goods.php';
				break;
			default:
				$r = HTTP_ROOT.'index.php?main_page='.$main_page;
		}
		return ($para=='')?$r:($r.(strstr($r, '?')?'&':'?').'&'.$para);
	}
}

function diy_execute($sql, $singleField = false){
	global $db;
	$obj = $db -> execute($sql);
	$r = array();
	while(!$obj -> EOF){
		$r[] = ($singleField===false)?($obj -> fields):($obj -> fields[$singleField]);
		$obj -> moveNext();
	}
	return $r;
}

function db_prepare($str){
	$str = str_replace("\\", '\\\\', $str);
	$str = str_replace('"', '\"', $str);
	$str = str_replace("'", "\'", $str);
	return $str;
}

function goods2html($goods){
	global $total_pages,$page,$cateId,$allCates;
	if (empty($goods)){
		$r = '<div class="hero-unit noGoods"><div class="span4 notice"><img src="'.HTTP_TMP.'img/notice.png" /></div><div class="span8"><h4>没有找到产品</h4><p><br><a href="'.makeUrl("index").'" class="btn btn-success">返回主页</a> <a href="'.makeUrl("products").'" class="btn btn-warning">全部产品</a></p></div></div>';
	} else {	
		$r = '<div class="well productsLists">';
		foreach($goods as $k => $v){
			if (($k)%4 == 0){
				$r .= '<div class="row-fluid">';
			}
			if ($v != false){
				$url = makeUrl('pInfo', 'pID='.$v['goods_id']);
				$r .= '<li class="span3">';
				$r .= '<a class="img" href="'.$url.'"><img src="'.HTTP_ROOT.OFFICIAL_SHOP_DIR.$v["goods_thumb"].'" alt="HOLI 豪礼 '.$v["goods_name"].'" /></a>';
				$r .= '<a href="'.$url.'"><h3>'.$v["goods_name"].'</h3></a>';
				$r .= '<p class="price">'.$v["shop_price"].'</p>';
				if (isset($v["cat_id"]) && isset($allCates[$v["cat_id"]]['cat_name']) && !empty($allCates[$v["cat_id"]]['cat_name'])){
					$v["cat_id"] = $v["cat_id"]*1;
					if ($v["cat_id"]>0){
						$r .= '<p class="inCate">属于：<a href="'.makeUrl("products", "cateId=".$v["cat_id"]).'">'.$allCates[$v["cat_id"]]['cat_name'].'</a></p>';
					}
				}
				$r .= '</li>';
			}
			if (($k+1)%4 == 0){
				$r .= '</div>';
			}
		}
		
		$prevPage = ($page<2?1:($page-1));
		$nextPage = ($page>=$total_pages?$total_pages:($page+1));
		
		$r .= '<div class="pagination"><ul><li'.($prevPage==$page?" class=\"disabled\"":"").'><a href="'.makeUrl(MAIN_PAGE, 'page='.$prevPage).'">上一页</a></li>';
		for ($i=0; $i<$total_pages; $i++){
			$currPage = $i+1;
			$r .= '<li'.($currPage==$page?" class=\"disabled\"":"").'><a href="'.makeUrl(MAIN_PAGE, 'page='.$currPage).'">'.$currPage.'</a></li>';
		}
		$r .= '<li'.($nextPage==$page?" class=\"disabled\"":"").'><a href="'.makeUrl(MAIN_PAGE, 'page='.$nextPage).'">下一页</a></li></ul></div>';
	
		$r .= '</div>';
	}
	return $r;
}

function jumpTo($url = '', $code = ''){
	$url = ($url == '')?makeUrl('index'):$url;
	switch($code){
		case '404':
			header('HTTP/1.1 404 Not Found');
			header("status: 404 Not Found");
 			break;
		default:
			#301
			header("HTTP/1.1 301 Moved Permanently");
	}
	header("Location: $url"); 
}

function meta_get_title($main_page = '', $add = ''){
	$shopName = 'HOLI 豪礼中国官方网站';
	switch($main_page){
		case 'products':
		case 'pInfo':
			$r = $add .' - ' .$shopName;
			break;
		case 'searchResult':
			$r = '搜索：'.$_GET['k'].' - ' .$shopName;
			break;
		default:
			if (defined('META_TITLE') && (trim(META_TITLE) != '')){
				$r = META_TITLE . ' - ' . $shopName;
			} else {
				$r = $shopName;
			}
	}
	return $r;
}

// 浏览器友好的变量输出
function dump($var, $echo=true,$label=null, $strict=true)
{
    $label = ($label===null) ? '' : rtrim($label) . ' ';
    if(!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = "<pre>".$label.htmlspecialchars($output,ENT_QUOTES)."</pre>";
        } else {
            $output = $label . " : " . print_r($var, true);
        }
    }else {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if(!extension_loaded('xdebug')) {
            $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
            $output = '<pre>'. $label. htmlspecialchars($output, ENT_QUOTES). '</pre>';
        }
    }
    if ($echo) {
        echo($output);
        return null;
    }else
        return $output;
}


function getPageContFormDb($title = ''){
	global $db;
	if ($title == '') return false;
	$sql = "SELECT content FROM ".DB_PRE.DB_PRE_ECSHOP."article WHERE title = '".META_TITLE."' LIMIT 1";
	$obj = $db -> execute($sql);
	if ($obj -> recordCount()>0){
		$page_cont = $obj -> fields['content'];
	} else {
		$page_cont = false;
	}
	return $page_cont;
}